package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.store.dao.StoreProductNormDescriptionDao;
import com.zbkj.crmeb.store.model.StoreProductNormDescription;
import com.zbkj.crmeb.store.service.StoreProductNormDescriptionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
* @author lvmd
* @description StoreProductDescriptionServiceImpl 接口实现
* @date 22-02-11
*/
@Service
public class StoreProductNormDescriptionServiceImpl extends ServiceImpl<StoreProductNormDescriptionDao, StoreProductNormDescription> implements StoreProductNormDescriptionService {

    /**
     * 查询标准商品详情
     * @param normId
     * @return
     */
    @Override
    public String getProductNormDesc(Integer normId) {
        StoreProductNormDescription description = baseMapper.selectOne(Wrappers.<StoreProductNormDescription>lambdaQuery()
                .select(StoreProductNormDescription::getDescription)
                .eq(StoreProductNormDescription::getNormId, normId)
                .last("LIMIT 1"));
        if (Objects.nonNull(description)) {
            return description.getDescription();
        }
        return "";
    }

    /**
     * 保存商品详情
     * @param id
     * @param storeInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveStoreProductNormDescription(Integer id, String storeInfo) {
        if (null == storeInfo) {
            return false;
        }
        deleteStoreProductNormDescription(id);
        StoreProductNormDescription description = new StoreProductNormDescription(id, storeInfo);
        int i = baseMapper.insert(description);
        return i > 0;
    }

    /**
     * 删除商品详情
     * @param id
     * @return
     */
    public boolean deleteStoreProductNormDescription(Integer id) {
        LambdaQueryWrapper<StoreProductNormDescription> lmq = new LambdaQueryWrapper<>();
        lmq.eq(StoreProductNormDescription::getNormId, id);
        int i = baseMapper.delete(lmq);
        return i > 0;
    }

    /**
     * 修改商品详情
     * @param id
     * @param storeInfo
     * @return
     */
    @Override
    public boolean updateStoreProductNormDescription(Integer id, String storeInfo) {
        if (null == storeInfo) {
            return false;
        }
        LambdaUpdateWrapper<StoreProductNormDescription> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(StoreProductNormDescription::getNormId, id);
        wrapper.set(StoreProductNormDescription::getDescription, storeInfo);
        return this.update(wrapper);
    }

}

